(function() {
    angular.module('app').component('monitorDayComponent', {        
        templateUrl: 'app/components/monitor.day.component/monitor.day.component.html',
        controller: monitorDayComponent,
        controllerAs: 'vm',
        bindings: {
            targetId: '<',
            monitorDayType: '<' //type:shop or product
        }
    });

    monitorDayComponent.$inject = ['$scope', '$rootScope', 'monitorDayComponentService', 'util'];

    function monitorDayComponent($scope, $rootScope, monitorDayComponentService, util) {
        // console.log('商品销售量历史走势监控');

        var vm = this;
        //vm.targetId
        vm.option = {
            title: {
                text: '商品销售额历史走势监控',
                subtext: '',
                x: 'center'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    var str = '<div style = "width:130px;text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "width:130px;text-align: left;padding-left:10px;">' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].data) + '</div>';
                    }

                    return str;
                }
            },
            grid: {
                bottom: 0
            },
            legend: {
                data: [],
                x: 'right'
            },
            xAxis: {
                data: [],
                axisLabel: {
                    show: true
                }
            },
            grid: {
                top: '30px',
                left: '1%',
                right: '1%',
                bottom: '20',
                containLabel: true
            },
            yAxis: [{
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000).toFixed(2) + '万';
                            case 6:
                                return (value / 10000).toFixed(2) + '万';
                            case 7:
                                return (value / 10000).toFixed(2) + '万';
                            case 8:
                                return (value / 10000).toFixed(2) + '万';
                            case 9:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 10:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 11:
                                return (value / 100000000).toFixed(2) + '亿';
                            case 12:
                                return (value / 100000000).toFixed(2) + '亿';
                        }

                        return value;
                    }
                }
            }],
            series: [{
                name: '销售额',
                type: 'line',
                data: [],
                markPoint: {
                    symbol: 'circle'
                }
            }]
        };

        vm.dim = 'sellMoney';

        vm.redraw = typeDraw;

        var StoreData;//临时存储

        init();

        function init() {

            // console.log('targetId:'+vm.targetId+'monitorDayType:'+vm.monitorDayType);

            //店铺日监控数据
            if (vm.monitorDayType == 'shop') {

                getShop();
            }else{
                getProduct();
            }
        }



        function getShop() {
            monitorDayComponentService.getShop(vm.targetId).then(function(data) {

                StoreData = data;
                drawShop('sellMoney');

            });
        }

        function getProduct() {
            monitorDayComponentService.get(vm.targetId).then(function(data) {

                StoreData = data;
                drawProduct('sellMoney');

            });
        }

        
       
        
        //根据type  choose
        function typeDraw(dim) {
            vm.dim = dim;
            switch (vm.monitorDayType) {
                case 'shop':
                    drawShop(dim);
                    break;

                case 'product':                    
                    drawProduct(dim);
                    break;
                default:
                    break;
            }
        }

        function drawShop(dim) {
            var option = angular.copy(vm.option);
            option.xAxis.data = StoreData.x;

            if (dim == 'sellAmount') {
                option.series[0].data = StoreData.sellAmount;
                option.title.text = "店铺销售量历史走势监控"
                option.series[0].name = "销售量";
            } else {
                option.series[0].data = StoreData.sellMoney;
                option.title.text = "店铺销售额历史走势监控"
                option.series[0].name = "销售额";
            }
            $rootScope.$emit('echart_draw_line_EV', option, 'shop_day');
        }

        function drawProduct(dim) {
            var option = angular.copy(vm.option);
            option.xAxis.data = StoreData.x;

            if (dim == 'sellAmount') {
                option.series[0].data = StoreData.sellAmount;
                option.title.text = "商品销售量历史走势监控"
                option.series[0].name = "销售量";
            } else {
                option.series[0].data = StoreData.sellMoney;
                option.title.text = "商品销售额历史走势监控"
                option.series[0].name = "销售额";
            }
            $rootScope.$emit('echart_draw_line_EV', option, 'product_day');
        }

    }
})();
